import { Controller } from 'egg';

class UserController extends Controller {
    /**
     * 处理后台登录
     * @returns {Promise<void>}
     */
    public async login() {
        const { ctx } = this;
        let param: any = {};
        if (ctx.req.method === 'GET') {
            param = ctx.query;
        } else if (ctx.req.method === 'POST') {
            param = ctx.request.body;
        }
        const username = param.username;
        const password = param.password;
        if (!username || !password) {
            ctx.body = { code: 4, msg: '参数不全' };
            return;
        }
        const returnData: any = await this.service.adminService.login(param.username, param.password);
        const token = ctx.helper.genUUID();
        await this.updateToken(returnData.admin_id, token);
        ctx.body = { code: 1, msg: '登录成功', data: { token, admin_identity: returnData.admin_identity, admin_id: returnData.admin_id, admin_username: returnData.admin_username } };
    }

    /**
     * 登录成功后刷新token值
     * @param {string} admin_id
     * @param {string} token
     * @returns {Promise<void>}
     */
    public async updateToken(admin_id: string, token: string) {
        await this.service.adminService.updateToken(admin_id, token);
    }

    /**
     * 新增管理员
     */
    public async createAdmin() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_username: 'string',
                admin_password: 'string',
                admin_identity: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.createAdmin(request.body);
        ctx.body = ctx.helper.responseSuccess('新增成功');
    }

    /**
     * 删除管理员
     */
    public async deleteAdmin() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.deleteAdmin(request.body);
        ctx.body = ctx.helper.responseSuccess('删除成功');
    }

    /**
     * 查找管理员
     */
    public async searchAdminByParam() {
        const { ctx, ctx: { request } } = this;
        const admin = await this.ctx.service.adminService.searchAdminByParam(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', admin);
    }

    /**
     * 修改管理员
     */
    public async updateAdmin() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id: 'string',
                admin_password: 'string',
                admin_username: 'string',
                admin_identity: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.udpateAdmin(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 启用、停用管理员
     */
    public async startAndStopAdmin() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id: 'string',
                type: 'type',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await this.ctx.service.adminService.startAndStopAdmin(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

}

export default UserController;
